package com.example.service.impl;

import ch.qos.logback.classic.Logger;
import com.bstek.ureport.utils.DataUtils;
import com.example.entity.PointDefend;
import com.example.entity.PointFlow;
import com.example.entity.SpotCheckPlan;
import com.example.mapp.PointDefendMapp;
import com.example.mapp.SpotCheckItemMapp;
import com.example.mapp.SpotCheckPlanMapp;
import com.example.mapp.SpotObjectPlanMapp;
import com.example.service.PointDefendService;
import com.example.service.SpotCheckPlanService;
import com.example.util.DateUtils;
import com.example.util.ResultUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 维保人员抽查计划
 */
@Service
public class PointDefendServiceImpl implements PointDefendService {

	private final static Logger logger = (Logger) LoggerFactory.getLogger(PointDefendServiceImpl.class);

	@Autowired
	private PointDefendMapp pointDefendMapp;

	/**
	 * 查询积分维护表的所有数据
	 * spotCheckPlan
	 * @return
	 */

	@Override
	public Map<String, Object> findList(PointDefend pointDefend) {
		String message = "查询成功";
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			Page<PointDefend> page = PageHelper.startPage(pointDefend.getCurrentPage(), pointDefend.getPageSize());
			List<PointDefend> list = pointDefendMapp.findList(pointDefend);
			map.put("list", list);
			map.put("total", page.getTotal());
		}catch (Exception e){
			logger.error(e.getMessage());
			message="查询失败";
			return ResultUtil.returnError(message + e.getMessage(),pointDefend);
		}
		return ResultUtil.returnSuccess(message, map);
	}

	/**
	 * 保存数据
	 * @param pointDefend
	 * @return
	 */
	@Override
	public Map<String, Object> insertPointDefend(PointDefend pointDefend) {
		String message = "保存成功";

		try{
			// 查询一下 唯一标识不能为空
			PointDefend point = new PointDefend();
			point.setIdentification(pointDefend.getIdentification());

			List<PointDefend> list = pointDefendMapp.findList(point);
			if(list !=null && list.size()>0){
				message="唯一标识：" + pointDefend.getIdentification() +"已经重复，请重新输入！";
				return ResultUtil.returnError(message,pointDefend);
			}
			pointDefend.setCreateDate(DateUtils.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss"));

			pointDefendMapp.insertPointDefend(pointDefend);
		}catch (Exception e){
			logger.error(e.getMessage());
			message="保存失败";
			return ResultUtil.returnError(message + e.getMessage(),pointDefend);
		}
		return ResultUtil.returnSuccess(message, pointDefend);
	}

	/**
	 * 批量删除
	 * @param map
	 * @return
	 */
	@Override
	public Map<String, Object> deletePointDefend(Map map) {
		String message = "删除成功";

		try{
			if(map !=null && map.size()>0){
				List<String> idList = (List<String>) map.get("ids");

				for(String id:idList){
					pointDefendMapp.deletePointDefend(id);
				}
			}
		}catch (Exception e){
			logger.error(e.getMessage());
			message="删除失败";
			return ResultUtil.returnError(message + e.getMessage(),map);
		}
		return ResultUtil.returnSuccess(message, map);
	}

	/**
	 * 修改
	 * @param pointDefend
	 * @return
	 */
	@Override
	public Map<String, Object> updatePointDefend(PointDefend pointDefend) {
		String message = "删除成功";

		try{
			if(pointDefend !=null){
				pointDefend.setUpdateBy(pointDefend.getCreateBy());
				pointDefend.setUpdateDate(DateUtils.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss"));
				pointDefendMapp.updatePointDefend(pointDefend);
			}
		}catch (Exception e){
			logger.error(e.getMessage());
			message="删除失败";
			return ResultUtil.returnError(message + e.getMessage(),pointDefend);
		}
		return ResultUtil.returnSuccess(message, pointDefend);
	}
}
